package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("CrawlerdetectController")
public class CrawlerdetectController extends CmiEmaController {

	@RequestMapping("/listCrawlerdetects")
	public ModelAndView listCrawlerdetects(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = crawlerdetectPaginationService.updatePagination(request, pagination);
		Set<Crawlerdetect> crawlerdetects = crawlerdetectPaginationService.getPaginationCrawlerdetects(pagination);

		mav.addObject("crawlerdetects", crawlerdetects);
		mav.addObject("entityName", "crawlerdetect");
		mav.addObject("pagination", pagination);
		mav.setViewName("crawlerdetect/listCrawlerdetects.jsp");
		return mav;
	}

	public String listCrawlerdetects() {
		return "redirect:/listCrawlerdetects";
	}

	@RequestMapping("/viewCrawlerdetect")
	public ModelAndView viewCrawlerdetect(@RequestParam Integer crawlerdetectId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("crawlerdetect", crawlerdetectDAO.findCrawlerdetectByPrimaryKey(crawlerdetectId));
		mav.setViewName("crawlerdetect/viewCrawlerdetect.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteCrawlerdetects")
	public ModelAndView confirmDeleteCrawlerdetects(
			@RequestParam(value = "deleteCrawlerdetectsIds", required = true) Set<Integer> deleteCrawlerdetectsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Crawlerdetect> deleteCrawlerdetects = crawlerdetectDAO.findCrawlerdetectsByIds(deleteCrawlerdetectsIds);
		Set<Crawlerdetect> referencedCrawlerdetects = crawlerdetectService
				.loadReferenceCrawlerdetects(deleteCrawlerdetects);
		boolean hasReferenced;
		if (referencedCrawlerdetects.size() == 0) {
			mav.addObject("deleteEntitys", deleteCrawlerdetects);
			hasReferenced = false;
		} else {
			deleteCrawlerdetects.removeAll(referencedCrawlerdetects);
			mav.addObject("referencedEntitys", referencedCrawlerdetects);
			mav.addObject("deleteEntitys", deleteCrawlerdetects);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Crawlerdetect");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listCrawlerdetects");
		return mav;
	}

	@RequestMapping("/deleteCrawlerdetect")
	public String deleteCrawlerdetect(@RequestParam Integer crawlerdetectId) {
		Crawlerdetect crawlerdetect = crawlerdetectDAO.findCrawlerdetectByPrimaryKey(crawlerdetectId);
		crawlerdetectService.deleteCrawlerdetect(crawlerdetect);
		return "forward:/listCrawlerdetects";
	}

	@RequestMapping("/confirmDeleteCrawlerdetect")
	public ModelAndView confirmDeleteCrawlerdetect(@RequestParam Integer crawlerdetectId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("crawlerdetect", crawlerdetectDAO.findCrawlerdetectByPrimaryKey(crawlerdetectId));

		mav.setViewName("crawlerdetect/deleteCrawlerdetect.jsp");

		return mav;
	}

	@RequestMapping("/deleteCrawlerdetects")
	public String deleteCrawlerdetects(
			@RequestParam(value = "deleteCrawlerdetectsId", required = true) Set<Integer> deleteCrawlerdetectsId) {
		crawlerdetectService.deleteCrawlerdetects(deleteCrawlerdetectsId);
		return "forward:/listCrawlerdetects";
	}

	@RequestMapping("/editCrawlerdetect")
	public ModelAndView editCrawlerdetect(@RequestParam Integer crawlerdetectId) {

		ModelAndView mav = new ModelAndView();

		Crawlerdetect crawlerdetect = crawlerdetectDAO.findCrawlerdetectByPrimaryKey(crawlerdetectId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("crawlerdetect", crawlerdetect);

		// 把所有的引用crawlerdetect对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(crawlerdetectService.loadCrawlerdetectObjectReferencedSet(crawlerdetect));

		// 将编辑页面crawlerdetect添加到mav中
		mav.setViewName("crawlerdetect/editCrawlerdetect.jsp");
		return mav;
	}

	@RequestMapping("/newCrawlerdetect")
	public ModelAndView newCrawlerdetect() {
		ModelAndView mav = new ModelAndView();
		Crawlerdetect crawlerdetect = new Crawlerdetect();

		mav.addObject("crawlerdetect", crawlerdetect);

		// 把所有的引用crawlerdetect对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(crawlerdetectService.loadCrawlerdetectObjectReferencedSet(crawlerdetect));

		mav.addObject("newFlag", true);
		mav.setViewName("crawlerdetect/editCrawlerdetect.jsp");

		return mav;
	}

	@RequestMapping("/saveCrawlerdetect")
	public ModelAndView saveCrawlerdetect(@ModelAttribute Crawlerdetect crawlerdetect) {

		ModelAndView mav = new ModelAndView();

		Crawlerdetectstatus crawlerdetectstatusTemp = crawlerdetect.getCrawlerdetectstatus();
		Crawlerdetectstatus crawlerdetectstatus = crawlerdetectstatusTemp == null ? null
				: CmiHibernateProxyUtil
						.deproxy(crawlerdetectstatusDAO.findCrawlerdetectstatusById(crawlerdetectstatusTemp.getId()));

		Crawlerdetect existingCrawlerdetect = CmiHibernateProxyUtil.deproxy(crawlerdetectDAO
				.findCrawlerdetectByPhysicalPrimaryKey(crawlerdetect.getFilename(), crawlerdetect.getRownumber()));

		crawlerdetect.setCrawlerdetectstatus(crawlerdetectstatus);
		// 新建记录
		if (crawlerdetect.getId() == null) {
			if (crawlerdetect.equals(existingCrawlerdetect)) {

				mav.addAllObjects(crawlerdetectService.loadCrawlerdetectClassReferencedSet(crawlerdetect));
				// mav = editContinue(mav, crawlerdetect);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("crawlerdetect/editCrawlerdetect.jsp");
			} else {
				crawlerdetectService.saveCrawlerdetect(crawlerdetect);
				mav.setViewName("forward:/listCrawlerdetects");
			}
		}
		// 编辑记录
		else {
			Crawlerdetect originCrawlerdetect = CmiHibernateProxyUtil
					.deproxy(crawlerdetectDAO.findCrawlerdetectByPrimaryKey(crawlerdetect.getId()));
			if (!crawlerdetect.equals(originCrawlerdetect) && crawlerdetect.equals(existingCrawlerdetect)) {
				mav.addAllObjects(crawlerdetectService.loadCrawlerdetectClassReferencedSet(crawlerdetect));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("crawlerdetect/editCrawlerdetect.jsp");
			} else {
				crawlerdetectService.saveCrawlerdetect(crawlerdetect);
				mav.setViewName("forward:/listCrawlerdetects");
			}
		}
		return mav;
	}

	@RequestMapping("/crawlerdetectController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
